package com.wty.love;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.room.Room;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wty.love.database.DiaryDatabase;
import com.wty.love.database.dao.DiaryDao;
import com.wty.love.database.entity.Diary;
import com.wty.love.diary.AddDiaryActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static String SETDELETEPOSITION = "SETDELETEPOSITION";
    public static String DELETEONEDIARY = "DELETEONEDIARY";
    private boolean lalala = true;
    private int deletePosition = 0;
    private DiaryDatabase diaryDatabase;
    private ViewPager mainViewPager;
    private MainAdapter adapter;
    private List<View> viewList;
    private List<Diary> diaryList;
    public static DiaryDao diaryDao;
    private FloatingActionButton addDiary;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarColor(this, Color.rgb(250,250,250));
        setContentView(R.layout.activity_main);
        initDatabase();
        initViews();
        initData();
        initClick();
        initObserve();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!lalala){
            initData();
            adapter.notifyDataSetChanged();
        }
        deletePosition = 0;
    }

    private void initDatabase(){
        diaryDatabase = Room.databaseBuilder(this, DiaryDatabase.class,"diaryDatabase")
                .allowMainThreadQueries()
                .build();
        diaryDao = diaryDatabase.getDiaryDao();
    }

    private void initViews(){
        mainViewPager = findViewById(R.id.mainViewPager);
        addDiary = findViewById(R.id.addDiary);
    }

    private void initData(){
//        diaryList = new ArrayList<>();
        diaryList = diaryDao.getAllDiaryDesc0();

        int type = View.VISIBLE;
        if (diaryList.size()>0){
            type = View.GONE;
        }
        findViewById(R.id.showNull).setVisibility(type);

        viewList = new ArrayList<>();
        for (int i=0;i<diaryList.size();i++){
            LayoutInflater inflater = LayoutInflater.from(this);
            viewList.add(inflater.inflate(R.layout.main_cover,null,false));
        }
        adapter = new MainAdapter(this,diaryList,viewList);
        mainViewPager.setAdapter(adapter);

        if (deletePosition < diaryList.size()){
            mainViewPager.setCurrentItem(deletePosition);
        }
        lalala = false;
    }

    private void initClick(){
        addDiary.setOnClickListener(v->{
            startActivity(new Intent(this, AddDiaryActivity.class));
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void setStatusBarColor(Activity activity, int statusColor) {
        Window window = activity.getWindow();
        //取消状态栏透明
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //添加Flag把状态栏设为可绘制模式
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(statusColor);
        //设置系统状态栏处于可见状态
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        //让view不根据系统窗口来调整自己的布局
        ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.setFitsSystemWindows(mChildView, false);
            ViewCompat.requestApplyInsets(mChildView);
        }
    }

    private void initObserve(){
        LiveEventBus.get(MainActivity.SETDELETEPOSITION).observe(this,o->{
            deletePosition = (int)o;
        });
        LiveEventBus.get(DELETEONEDIARY).observe(this,position->{
            if (position==null) return;
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("删除")
                    .setMessage("确定要删除吗")
                    .setIcon(R.drawable.delete_diary_24)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            deleteDiary(Integer.parseInt(position.toString()));
                        }
                    })

                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) { }
                    })
                    .create();
            alertDialog.show();

        });
    }

    private void deleteDiary(int index){
        List<Diary> newDiaryList = new ArrayList<>();
        List<View> newViewList = new ArrayList<>();
        deletePosition = index;
        diaryDao.deleteDiaryByPosition(diaryList.get(index));
        for (int i=0;i<diaryList.size();i++){
            if (i!=index){
                newDiaryList.add(diaryList.get(i));
                newViewList.add(viewList.get(i));
            }
        }
        diaryList = newDiaryList;
        viewList = newViewList;

        adapter.setDiaryList(diaryList);
        adapter.setViewList(viewList);
        adapter.notifyDataSetChanged();
        initData();
        Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
    }
}